package com.gluck.cases.core.block.impl;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import com.gluck.cases.common.utils.dal.dao.CasesApiMapper;
import com.gluck.cases.common.utils.utils.AssertUtil;
import com.gluck.cases.core.block.CasesApiBlock;
import com.gluck.cases.core.modal.assemble.CasesApiAssemble;
import com.gluck.cases.core.modal.assemble.PageInfoAssemble;
import com.gluck.cases.core.modal.dto.CasesApiDTO;
import com.gluck.cases.core.modal.entity.CasesApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CasesApiBlockImpl
 * @Desc CasesApiBlock 实现类
 * @Author shiyongzhe
 * @Date 2022/5/28 - 5:32 下午
 **/
@Service
public class CasesApiBlockImpl implements CasesApiBlock {

    @Autowired
    private CasesApiMapper casesApiMapper;

    @Override
    public boolean insert(CasesApi api) {
        int insert = casesApiMapper.insert(api);
        AssertUtil.isTrue(insert == 0, BizSerErrorEnum.INSERT_ERROR);
        return true;
    }

    @Override
    public CasesApiDTO queryById(Long id) {
        CasesApi casesApi = casesApiMapper.queryById(id);
        return CasesApiAssemble.CasseApiConvertDTO(casesApi);
    }

    @Override
    public PageInfo<CasesApiDTO> queryByCondition(CasesApiDTO dto, int page, int size) {
        PageInfo<CasesApi> cases = new PageInfo<>(PageHelper.startPage(page, size).doSelectPage(new ISelect() {
            @Override
            public void doSelect() {
                casesApiMapper.queryByCondition(dto);
            }
        }));
        PageInfoAssemble<CasesApiDTO> result = new PageInfoAssemble<>();
        List<CasesApiDTO> collect = cases.getList().stream().map(x -> {
            return CasesApiAssemble.CasseApiConvertDTO(x);
        }).collect(Collectors.toList());
        PageInfo<CasesApiDTO> dtoPageInfo = result.covertDtoPageInfo(cases, collect);
        return dtoPageInfo;
    }

    @Override
    public boolean updateByCondition(CasesApiDTO dto) {
        int update = casesApiMapper.updateByCondition(dto);
        return update > 0;
    }

    @Override
    public boolean deleteById(Long id) {
        int delete = casesApiMapper.deleteById(id);
        return delete > 0;
    }


    @Override
    public List<CasesApiDTO> queryCasesList() {
        CasesApiDTO dto = new CasesApiDTO();
        dto.setPid(0l);
        List<CasesApi> casesApis = casesApiMapper.queryByCondition(dto);
        if (casesApis.size() == 0) {
            return Collections.emptyList();
        }
        List<CasesApiDTO> dtos = CasesApiAssemble.CasesApiConvertDtoList(casesApis);
        dtos.stream().forEach(x -> {
            List<CasesApiDTO> casesApiDTOS = recursionCasesByPid(x.getId());
            x.setChildren(casesApiDTOS);
        });
        return dtos;
    }

    private List<CasesApiDTO> recursionCasesByPid(Long id) {
        CasesApiDTO dto = new CasesApiDTO();
        dto.setPid(id);
        List<CasesApi> casesApis = casesApiMapper.queryByCondition(dto);
        List<CasesApiDTO> childrenDtos = CasesApiAssemble.CasesApiConvertDtoList(casesApis);
        childrenDtos.stream().map((child) -> {
            List<CasesApiDTO> dtos = recursionCasesByPid(child.getId());
            child.setChildren(dtos);
            return child;
        }).collect(Collectors.toList());
        return childrenDtos;
    }
}
